package com.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

import com.coder.Coder;
import com.coder.CoderField;
import com.jdbc.GetTableInfoUtil;


public class ConvertUtil {
	
	//����_�� Ҫ���»���  ��hhhh_aaaa-> hhhhAaaa
	public static String converUpperCaseFirst(String columnName)
    {
		if (columnName != null)
		{
			StringBuffer sb = new StringBuffer();
			StringBuffer temp = null;
			
			if (columnName.contains("_")) 
			{
				columnName = columnName.toLowerCase();
				String [] columnNameArray = columnName.split("_");
				for (int i = 0; i < columnNameArray.length; i++) 
				{
					if (i != 0) 
					{
						 temp = new StringBuffer(columnNameArray[i]);
						 temp.setCharAt(0, Character.toUpperCase(columnNameArray[i].charAt(0)));
						 columnNameArray[i] = temp.toString();
					}
					sb.append(columnNameArray[i]);
					temp = null;
				}
			}
			else
			{
				sb.append(columnName.substring(0, 1).toLowerCase() + columnName.substring(1));
			}
			return sb.toString();
		} 
		else
		{
			return columnName;
		}
    }
	
	public static String firstToUpCase(String str)
	{
		StringBuffer sb = new StringBuffer("");
		if (str !=  null) 
		{
			sb.append(str.substring(0, 1).toUpperCase() + str.substring(1));
		}
		return sb.toString();
	}
	
	public static String removePrefix(String prefix ,String str)
	{
		if (str.startsWith(prefix)) 
		{
			str = str.substring(prefix.length(),str.length());
		}
		return str;
	}
	
	public static void main(String[] args) 
	{
		System.out.println(converUpperCaseFirst("yyyh_aBCd"));
		System.out.println(converUpperCaseFirst("YyyhaBCd"));
		System.out.println(removePrefix("yyyh","yyyh_aBCd"));
	}
	
	/**
	 * ���շ�ʽ������ַ�ת��Ϊ�»��ߴ�д��ʽ�����ת��ǰ���շ�ʽ������ַ�Ϊ�գ��򷵻ؿ��ַ�</br>
	 * ���磺HelloWorld->HELLO_WORLD
	 * @param name ת��ǰ���շ�ʽ������ַ�
	 * @return ת�����»��ߴ�д��ʽ������ַ�
	 */
	public static String underscoreName(String name) {
		StringBuilder result = new StringBuilder();
		if (name != null && name.length() > 0) {
			// ����һ���ַ���ɴ�д
			result.append(name.substring(0, 1).toUpperCase());
			// ѭ�����������ַ�
			for (int i = 1; i < name.length(); i++) {
				String s = name.substring(i, i + 1);
				// �ڴ�д��ĸǰ����»���
				if (s.equals(s.toUpperCase())
						&& !Character.isDigit(s.charAt(0))) {
					result.append("_");
				}
				// �����ַ�ֱ��ת�ɴ�д
				result.append(s.toUpperCase());
			}
		}
		return result.toString();
	}

	/**
	 * ���»��ߴ�д��ʽ������ַ�ת��Ϊ�շ�ʽ�����ת��ǰ���»��ߴ�д��ʽ������ַ�Ϊ�գ��򷵻ؿ��ַ�</br>
	 * ���磺HELLO_WORLD->HelloWorld
	 * @param name ת��ǰ���»��ߴ�д��ʽ������ַ�
	 * @return ת������շ�ʽ������ַ�
	 */
	public static String camelName(String name) {
		StringBuilder result = new StringBuilder();
		// ���ټ��
		if (name == null || name.isEmpty()) {
			// û��Ҫת��
			return "";
		} else if (!name.contains("_")) {
			// �����»��ߣ���������ĸСд
			return name.substring(0, 1).toLowerCase() + name.substring(1);
		}
		// ���»��߽�ԭʼ�ַ�ָ�
		String camels[] = name.split("_");
		for (String camel : camels) {
			// ���ԭʼ�ַ��п�ͷ����β���»��߻�˫���»���
			if (camel.isEmpty()) {
				continue;
			}
			// ����������շ�Ƭ��
			if (result.length() == 0) {
				// ��һ���շ�Ƭ�Σ�ȫ����ĸ��Сд
				result.append(camel.toLowerCase());
			} else {
				// ������շ�Ƭ�Σ�����ĸ��д
				result.append(camel.substring(0, 1).toUpperCase());
				result.append(camel.substring(1).toLowerCase());
			}
		}
		return result.toString();
	}
	
	/**
	 * @throws Exception 
	 * map �е�yssh_user_role:{"yssh_user_role":[{"javaType":"java.lang.Long","dbType":"BIGINT","columnName":"user_id","columnLen":20},
	 *                                           {"javaType":"java.lang.Long","dbType":"BIGINT","columnName":"role_id","columnLen":20}]}
	 */
	public static Coder converterObject(String key,String mapValue,String moduleName,String shengcPath,String vueFileUrl) throws Exception
	{
		JSONObject jsonObject = null;
		JSONObject tableObj = null;
		JSONArray columnObJsonArray = null;
		tableObj = new JSONObject(mapValue);
		List<CoderField> coderFields = new ArrayList<CoderField>();
		columnObJsonArray = tableObj.getJSONArray(key);
		for (int i = 0; i < columnObJsonArray.length(); i++) 
		{
			jsonObject = columnObJsonArray.getJSONObject(i);
			CoderField field = new CoderField();
			field.setName(converUpperCaseFirst(jsonObject.getString("columnName")));
			String str = jsonObject.getString("javaType");
			str = str.substring(str.lastIndexOf(".") + 1);
			field.setType(str);
			field.setTableFieldName(jsonObject.getString("columnName"));
			coderFields.add(field);
		}
		Coder coder = new Coder();
		coder.setModelName(firstToUpCase(converUpperCaseFirst(key)));
		coder.setModuleName(moduleName);
		coder.setTableName(key);
		coder.setFilePath(shengcPath);
		coder.setVuefilePath(vueFileUrl);
		coder.setFields(coderFields);
		return coder;
	}
	
	
	public static List<Coder> getCoderList(Map<String,String> map,String moduleName,String shengcPath,String vueFileUrl) throws Exception 
	{
		List<Coder> coders = new ArrayList<Coder>();
		Coder coder = new Coder();
		Set<String> keys = map.keySet();
		for (String key : keys) 
		{
			coder = converterObject(key,map.get(key),moduleName,shengcPath,vueFileUrl);
			coders.add(coder);
		}
		return coders;
	}
}
